﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SalesModel;
using pe.edu.pucp.lp2.controller;
using SalesController;

namespace SalesApp
{
    public partial class VentaForm : Form
    {
        Sale sale = new Sale();
        List<SalesDetail> listaProd = new List<SalesDetail>();
        int cont = 1;
        double total = 0;

        public VentaForm()
        {
            InitializeComponent();
            sale.setRegDate(DateTime.Now);
            sale.setTax(0.19);
        }

        public void RefreshGridProducts()
        {
            dgvProducts.Rows.Clear();

            List<Product> list = SalesManager.getAllProducts();

            for (int i = 0; i < list.Count; i++)
            {
                dgvProducts.Rows.Add(new String[] { "" + list[i].getId_product(),
                                                    list[i].getName(),
                                                    list[i].getBrand(),
                                                    list[i].getDescription(),
                                                    "" + list[i].getBasePrice(),
                                                    list[i].getCategory(),
                                                    list[i].getEan()});
            }
        }

        public void RefreshGridBoleta()
        {
            dgvBoleta.Rows.Clear();
            for (int i = 0; i < listaProd.Count; i++)
                dgvBoleta.Rows.Add(new String[]{ "" + listaProd[i].getIdDetail(),
                                                      listaProd[i].getProduct().getDescription(),
                                                 "" + listaProd[i].getQuantity(),
                                                 "" + listaProd[i].getSubTotal(),
                                            });
        }

        private void VentaForm_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'inf282DataSet5.BoolSaleLineItem' Puede moverla o quitarla según sea necesario.
            //this.boolSaleLineItemTableAdapter.Fill(this.inf282DataSet5.BoolSaleLineItem);
            RefreshGridBoleta();
            RefreshGridProducts();
        }

        private void dgvProducts_Click(object sender, EventArgs e)
        {
           
        }
        private void btnAddToInvoice_Click(object sender, EventArgs e)
        {
            
            int selRow = dgvProducts.SelectedRows[0].Index;
            int id = Int32.Parse(dgvProducts.Rows[selRow].Cells[0].Value.ToString());
            Product p = SalesManager.getProductById(id);

            if (Int32.Parse(txtQuantity.Text) <= p.getStock())
            {

                SalesDetail sd = new SalesDetail();

                sd.setProduct(p);
                sd.setQuantity(Int32.Parse(txtQuantity.Text));
                sd.setSubTotal(Int32.Parse(txtQuantity.Text) * p.getBasePrice());
                sd.setIdDetail(cont);
                cont++;
                listaProd.Add(sd);
                RefreshGridBoleta();

                for (int i = 0; i < listaProd.Count; i++)
                    total += listaProd[i].getSubTotal();

                txtTotal.Text = "" + total;
            }
            else
            {
                MessageBox.Show("Se sobrepasa el stock del producto",
                "Alerta",
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
            }
        }

        private void btnEndSale_Click(object sender, EventArgs e)
        {
            Cashier cs = SalesManager.getCashierById(Int32.Parse(txtCashierId.Text));
            sale.setCashier(cs);
            sale.setRegMachine(Int32.Parse(txtCashierTerminalId.Text));
            sale.setRegDate(DateTime.Now);
            sale.setTax(0.19);
            sale.setTotalAmmount(total);
            for (int i = 0; i < listaProd.Count; i++)
                sale.addSaleDetail(listaProd[i].getProduct(),listaProd[i].getQuantity());
            SalesManager.addSale(sale);

            for (int i = 0; i < listaProd.Count; i++)
                SalesManager.addSaleDetail(listaProd[i],sale.getId_sale());

                for (int i = 0; i < listaProd.Count; i++)
                {
                    Product p = new Product();
                    int stock = 0;
                    p = SalesManager.getProductById(listaProd[i].getProduct().getId_product());
                    stock = p.getStock();
                    stock -= listaProd[i].getQuantity();
                    if (stock < 0) stock = 0;
                    p.setStock(stock);
                    SalesManager.updateProduct(p);
                }
        }
    }
}
